﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

using Shove.Alipay;

public partial class OnlinePay_Alipay02_Send2 : PageBase
{
    private string gateway = "";                         
    private string service = "";
    private string partner = "";
    private string sign_type = "";
    private string out_trade_no = "";
    private string subject = "";
    private string body = "";
    private string payment_type = "";
    private string total_fee = "";
    private string show_url = "";
    private string seller_email = "";
    private string key = "";
    private string return_url = "";
    private string _input_charset = "";
    private string notify_url = "";
    private string buyer_email = "";
    private string paymethod = "";

    public double Money = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        double Money = Shove._Convert.StrToDouble(Shove._Web.Utility.GetRequest("PayMoney"), 0);
        string BankCode = Shove._Web.Utility.GetRequest("BankCode");

        int RelatedOrderType = Shove._Convert.StrToInt(Shove._Web.Utility.GetRequest("RelatedOrderType"), -1);
        long RelatedOrderID = Shove._Convert.StrToLong(Shove._Web.Utility.GetRequest("RelatedOrderID"), -1);

        if (!IsPostBack)
        {
            SystemOptions so = new SystemOptions();                                                                          //读取系统设置tbl_Options里的总站支付配置信息,若要设置店铺的支付设置,请查询tbl_Shop表
            long ShopID = _Site.ID;                                                                                          //店铺ID

            double FormalitiesFeesScale = so["OnlinePay_Alipay_PayFormalitiesFeesScale"].ToDouble(0) / 100;                  //手续费
            double FormalitiesFees = Math.Round(Money * FormalitiesFeesScale, 2);

            Money += FormalitiesFees;

            partner = so["OnlinePay_Alipay_UserNumber"].ToString("");                                                       //卖家商户号
            seller_email = so["OnlinePay_Alipay_UserName"].ToString("");                                                    //卖家支付宝名称(邮箱)
            key = so["OnlinePay_Alipay_MD5Key"].ToString("");
            return_url = Shove._Web.Utility.GetUrl() + "/OnlinePay/Alipay02/Receive.aspx";                                  //服务器通知返回接口
            notify_url = Shove._Web.Utility.GetUrl() + "/OnlinePay/Alipay02/AlipayNotify.aspx";                             //服务器通知返回接口(暂时不用


            #region

            //Sites NewSites = null;
            ////业务参数赋值；

            //short _CustomerType = _Customer.CustomerType;
            //long ShopID = _Customer.SiteID;

            ////0、内部员工 1、总公司 2、分公司 3、酒庄 4、酒道 5、业务员 6、生产商 7、普通会员
            //if (!_Customer.isOwnerCustomer)
            //{
            //    if ((_CustomerType == 0) || (_CustomerType == 6) || (_CustomerType == 7))
            //    {
            //        NewSites = new Sites()[_Customer.ShopID2];

            //        partner = s["Opt_OnlinePay_Alipay_UserNumber"].ToString("");                                 //卖家商户号
            //        seller_email = NewSites.SiteOptions["Opt_OnlinePay_Alipay_UserName"].ToString("");                              //卖家支付宝名称(邮箱)
            //        key = s["Opt_OnlinePay_Alipay_MD5Key"].ToString("");

            //        return_url = Shove._Web.Utility.GetUrl() + "/OnlinePay/AlipayForShop/Receive.aspx";                                     //服务器通知返回接口
            //        notify_url = Shove._Web.Utility.GetUrl() + "/OnlinePay/AlipayForShop/AlipayNotify.aspx";                                //服务器通知返回接口(暂时不用

            //        ShopID = _Customer.ShopID2;
            //    }
            //    else
            //    {
            //        NewSites = new Sites()[1];          //站点的所有者充值，只能充到总站

            //        partner = s["Opt_OnlinePay_Alipay_UserNumber"].ToString("");                                 //卖家商户号
            //        seller_email = s["Opt_OnlinePay_Alipay_UserName"].ToString("");                              //卖家支付宝名称(邮箱)
            //        key = s["Opt_OnlinePay_Alipay_MD5Key"].ToString("");
            //        return_url = Shove._Web.Utility.GetUrl() + "/OnlinePay/Alipay02/Receive.aspx";                                     //服务器通知返回接口
            //        notify_url = Shove._Web.Utility.GetUrl() + "/OnlinePay/Alipay02/AlipayNotify.aspx";                                //服务器通知返回接口(暂时不用

            //        ShopID = 1;
            //    }
            //}
            //else
            //{
            //    NewSites = new Sites()[1];          //站点的所有者充值，只能充到总站

            //    partner = s["Opt_OnlinePay_Alipay_UserNumber"].ToString("");                                 //卖家商户号
            //    seller_email = s["Opt_OnlinePay_Alipay_UserName"].ToString("");                              //卖家支付宝名称(邮箱)
            //    key = s["Opt_OnlinePay_Alipay_MD5Key"].ToString("");
            //    return_url = Shove._Web.Utility.GetUrl() + "/OnlinePay/Alipay02/Receive.aspx";                                     //服务器通知返回接口
            //    notify_url = Shove._Web.Utility.GetUrl() + "/OnlinePay/Alipay02/AlipayNotify.aspx";                                //服务器通知返回接口(暂时不用

            //    ShopID = 1;
            //}

            #endregion

            gateway = "https://www.alipay.com/cooperate/gateway.do?";	                                                       //支付接口
            service = "create_direct_pay_by_user";
            sign_type = "MD5";
            payment_type = "1";                                                                                                //支付类型
            _input_charset = "utf-8";                                                                                          //编码类型
            show_url = "http://www.alipay.com";
            paymethod = "bankPay";                                                                                             //赋值:bankPay(网银);cartoon(卡通); directPay(余额) 三种付款方式都要，参数为空
            buyer_email = _Customer.Email;
            subject = Shove._Security.Encrypt.EncryptString(PublicFunction.GetCallCert(), _Customer.ID.ToString());	           //商品名称,作为加密的用户ID用
            body = "货款";                                                                                                     //商品描述
            double PayMoney = Convert.ToDouble(Money.ToString());
            total_fee = PayMoney.ToString();                                                                                   //总金额 0.01～50000.00     

            long NewPayNumber = -1;
            string ReturnDescription = "";

            if (DAL.Procedures.P_Alipay_GetNewPayNumber(ShopID, _Customer.ID, "Alipay", (PayMoney - FormalitiesFees), FormalitiesFees, RelatedOrderType, RelatedOrderID, ref NewPayNumber, ref ReturnDescription) < 0)
            {
                PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据库读写错误", this.Page.GetType().FullName);

                return;
            }

            if ((NewPayNumber < 0) || (ReturnDescription != ""))
            {
                PublicFunction.GoError(ErrorNumber.Unknow, ReturnDescription, this.Page.GetType().FullName);

                return;
            }

            out_trade_no = NewPayNumber.ToString();

            if (BankCode.ToLower() == "alipay")
            {
                AlipayPay();
            }
            else
            {
                BankClick(BankCode);
            }
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;

        base.OnLoad(e);
    }

    #endregion

    protected void BankClick(string BankCode)
    {
        Alipay ap = new Alipay();

        string aliay_url = ap.CreatUrl(
            gateway,
            service,
            partner,
            return_url,
            notify_url,
            out_trade_no,
            subject,
            payment_type,
            total_fee,
            seller_email,
            key,
            _input_charset,
            sign_type,
            "body",
            body,
            "defaultbank",
            BankCode,
            "paymethod",
            paymethod
            );

        if (aliay_url == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "地址构建出现错误");

            return;
        }

        this.Response.Write("<script language='javascript'>window.top.location.href='" + aliay_url + "'</script>");
    }

    protected void AlipayPay()
    {
        Alipay ap = new Alipay();

        string aliay_url = ap.CreatUrl(
            gateway,
            service,
            partner,
            return_url,
            notify_url,
            out_trade_no,
            subject,
            payment_type,
            total_fee,
            seller_email,
            key,
            _input_charset,
            sign_type,
            "body",
            body,
            "show_url",
            show_url,
            "paymethod",
            paymethod
            );

        if (aliay_url == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "地址构建出现错误");

            return;
        }

        this.Response.Write("<script language='javascript'>window.top.location.href='" + aliay_url + "'</script>");
    }
}